//
//  HomePageViewController.m
//  XiangCaoJiaYuan
//
//  Created by tongj on 2017/8/15.
//  Copyright © 2017年 tongj. All rights reserved.
//

#import "HomePageViewController.h"
#import "UIButton+LT.h"
#import "MineViewController.h"//我的界面
#import "HelpViewController.h"//帮助界面
#import "RankViewController.h"//排行榜
#import "PostViewController.h"//发布界面
#import "BYIndicatorView.h"
#import "BYRadarView.h"
#import "ResultViewController.h"

@interface HomePageViewController ()
{
    NSString *_setTimeString;
    NSArray *_timeArray;
    UIImageView* _imgViewPerson;
}
@property(nonatomic,strong)UILabel* DesLab;//领取红包描述
@property(nonatomic,strong)UILabel* PersonNumLab;//多少人参加
@property(nonatomic,strong)UILabel* ReceiveLab;//领取人数

@property(nonatomic,strong)UIImageView* RedimgView;//预计发红包底图
@property (nonatomic, strong)UIImageView * bView;
@property (nonatomic, strong)UIImageView * iconImageView;

@property(nonatomic,strong)UILabel* labelTime;
@property(nonatomic,strong)UIImageView* img;
@end

@implementation HomePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationItem setHidesBackButton:YES];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationController.navigationBar.backItem setHidesBackButton:YES];
    self.title = @"香草园地";
    [self CreateUI];
    
    
//    _setTimeString = @"18:00:00";
    
    _timeArray = [_setTimeString componentsSeparatedByString:@":"];
    
    [self refreshTime];
    
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(refreshTime) userInfo:nil repeats:YES];
    
    _img = [[UIImageView alloc]init];
    _img.frame = CGRectMake(WIDTH*85,UIScreen_HEIGHT-210*HEIGHT-64,200*WIDTH,HEIGHT*82);
    _img.image = [UIImage imageNamed:@"倒计时_03"];
    [_RedimgView addSubview:_img];
    
    _labelTime = [[UILabel alloc]init];
    _labelTime.font = [UIFont systemFontOfSize:17*WIDTH];
    _labelTime.frame = CGRectMake(11*WIDTH,50*HEIGHT,180*WIDTH,30*HEIGHT);
    _labelTime.textColor = color3cb83a;
    _labelTime.textAlignment = NSTextAlignmentLeft;
    [_img addSubview:_labelTime];
}
-(void)viewWillAppear:(BOOL)animated{
    NSString *url = [USERDEFAULT objectForKey:@"head_image"];
    [_imgViewPerson sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"香草园地_10"]];
    [_iconImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"香草园地_10"]];

}
-(void)CreateUI
{
    
    _RedimgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,UIScreen_WIDTH, UIScreen_HEIGHT)];
    _RedimgView.image = [UIImage imageNamed:@"E839B"];
    _RedimgView.userInteractionEnabled = YES;
    [self.view addSubview:_RedimgView];
    
    
    _DesLab = [[UILabel alloc]init];
    _DesLab.frame = CGRectMake(10, 20, 227*WIDTH, 38*HEIGHT);
    _DesLab.text = @"已有3000人参与 300人领取红包";
    _DesLab.textAlignment = NSTextAlignmentCenter;
    _DesLab.backgroundColor = colorf2a002;
    _DesLab.font = [UIFont systemFontOfSize:14*WIDTH];
    _DesLab.layer.masksToBounds = YES;
    _DesLab.layer.cornerRadius = 19*WIDTH;
    _DesLab.textColor = [UIColor whiteColor];
    [_RedimgView addSubview:_DesLab];
    
    UIButton* but = [UIButton buttonWithType:UIButtonTypeCustom];
    but.frame = CGRectMake(UIScreen_WIDTH-8-93*WIDTH,20,93*WIDTH,HEIGHT*35);
    [but setImage:[UIImage imageNamed:@"倒计时_03(1)"] forState:UIControlStateNormal];
    [but addTarget:self action:@selector(Click) forControlEvents:UIControlEventTouchUpInside];
    [_RedimgView addSubview:but];
    

    
    //雷达图效
    UIView* lei = [[UIView alloc]initWithFrame:CGRectMake(58*WIDTH, 9+but.frame.size.height+HEIGHT*17+15*HEIGHT+24*HEIGHT, UIScreen_WIDTH-2*58*WIDTH, UIScreen_WIDTH-2*58*WIDTH)];
    [_RedimgView addSubview:lei];
    
    _bView = [[UIImageView alloc] initWithFrame:CGRectMake(0 ,0,lei.frame.size.width, lei.frame.size.height)];
    _bView.userInteractionEnabled = YES;
    _bView.backgroundColor = nil;
    _bView.image = [UIImage imageNamed:@"leidao_07"];
    [lei addSubview:_bView];
    

    BYRadarView * radView = [[BYRadarView alloc] initWithFrame:_bView.bounds];
    radView.backgroundColor = [UIColor clearColor];
    [_bView addSubview:radView];
    
    BYIndicatorView * view = [[BYIndicatorView alloc] initWithFrame:_bView.bounds];
    view.backgroundColor = [UIColor clearColor];
    [_bView addSubview:view];
    
    _iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _bView.frame.size.width * .25, _bView.frame.size.width * .25)];
    _iconImageView.center = _bView.center;
    _iconImageView.layer.cornerRadius = _bView.frame.size.width * .25 / 2;
    _iconImageView.layer.masksToBounds = YES;
    _iconImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    _iconImageView.layer.borderWidth = 2.0;
    _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
//    _iconImageView.image =[UIImage imageNamed:@"9A9B"];
    //    _iconImageView.backgroundColor = [UIColor orangeColor];
    [lei addSubview:_iconImageView];
    
    
//    香草园地_14
//    UIButton*butMM = [UIButton buttonWithType:UIButtonTypeCustom];
//    butMM.frame = CGRectMake(UIScreen_WIDTH-95*WIDTH,UIScreen_HEIGHT-250*HEIGHT-64,95*WIDTH,HEIGHT*50);
//    [butMM setImage:[UIImage imageNamed:@"香草园地_14"] forState:UIControlStateNormal];
//    [butMM addTarget:self action:@selector(ButMM) forControlEvents:UIControlEventTouchUpInside];
//    [_RedimgView addSubview:butMM];
    _imgViewPerson = [[UIImageView alloc]initWithFrame:CGRectMake(10,UIScreen_HEIGHT-10-73*HEIGHT-64,73*WIDTH,WIDTH*73)];
    _imgViewPerson.userInteractionEnabled = YES;
    _imgViewPerson.clipsToBounds = YES;
    _imgViewPerson.layer.cornerRadius = 36.5*WIDTH;
    _imgViewPerson.contentMode = UIViewContentModeScaleAspectFill;
    [_RedimgView addSubview:_imgViewPerson];
    
    NSArray* arr = @[@"发布",@"消息",@"我的",@"帮助"];
    for (int i =0; i<4; i++) {
        
        UILabel* lab = [[UILabel alloc]initWithFrame:CGRectMake(10+_imgViewPerson.frame.size.width+WIDTH*18+75*WIDTH*i, UIScreen_HEIGHT-25*HEIGHT-64,35*WIDTH, 15*HEIGHT)];
        lab.font = [UIFont systemFontOfSize:15*HEIGHT];
        lab.text = arr[i];
        lab.adjustsFontSizeToFitWidth = YES;
        lab.textColor = color333333;
        [_RedimgView addSubview:lab];
        
    }
    NSArray* arrBut = @[@"发布-",@"消息-",@"我的-",@"帮助.-"];
    for (int i =0; i<4; i++) {
        
        UIButton* imgBut = [UIButton buttonWithType:UIButtonTypeCustom];
        imgBut.tag = i+1;
        [imgBut setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",arr[i]]] forState:UIControlStateNormal];
        [imgBut setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@",arrBut[i]]] forState:UIControlStateHighlighted];
        imgBut.frame = CGRectMake(10+_imgViewPerson.frame.size.width+WIDTH*17+75*WIDTH*i, UIScreen_HEIGHT-25*HEIGHT-8-32*HEIGHT-64, 32*WIDTH,32*HEIGHT);
        [imgBut addTarget:self action:@selector(Imgclick:) forControlEvents:UIControlEventTouchUpInside];
        [_RedimgView addSubview:imgBut];
    }
}

#pragma mark 排行榜
-(void)Click
{
//    ResultViewController* resVC = [[ResultViewController alloc]init];
//    [self presentViewController:resVC animated:YES completion:nil];
//    [self.navigationController pushViewController:resVC animated:NO];
    
    RankViewController* ranlVC = [[RankViewController alloc]init];
    [self.navigationController pushViewController:ranlVC animated:NO];
}
#pragma mark 美眉窗口
-(void)ButMM
{
    
}
-(void)Imgclick:(UIButton*)sender
{
    if (sender.tag ==1) {
        PostViewController* postVC = [[PostViewController alloc]init];
        [self.navigationController pushViewController:postVC animated:NO];

    }
    else if (sender.tag==2){
        
        //环信ID:@"8001"
        //聊天类型:EMConversationTypeChat
        EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:@"8001" conversationType:EMConversationTypeChat];
        [self.navigationController pushViewController:chatController animated:YES];
        
    }else if (sender.tag==3){
        MineViewController* MineVC = [[MineViewController alloc]init];
        [self.navigationController pushViewController:MineVC animated:NO];

    }else
    {
        HelpViewController* helpVC =[[HelpViewController alloc]init];
        [self.navigationController pushViewController:helpVC animated:NO];
        
    }
    
}



- (void)refreshTime
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond |NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitQuarter |NSCalendarUnitWeekOfMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitYearForWeekOfYear fromDate:[NSDate date]];
    
    NSCalendar *todayCalender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *todayDateComponents = [[NSDateComponents alloc] init];
    todayDateComponents.year = dateComponents.year;
    todayDateComponents.month = dateComponents.month;
    todayDateComponents.day = dateComponents.day;
    todayDateComponents.hour = [_timeArray[0] integerValue];
    todayDateComponents.minute = [_timeArray[1] integerValue];
    todayDateComponents.second = [_timeArray[2] integerValue];
    
    
    NSDate *todayDate = [todayCalender dateFromComponents:todayDateComponents];
    
    NSDateComponents *betweenDate = [todayCalender components:NSCalendarUnitSecond fromDate:[NSDate date] toDate:todayDate options:0];
    
    NSString *betweenTime;
    // 已经过时
    if (betweenDate.second < 0)
    {
        
        
//        ResultViewController* resVC = [[ResultViewController alloc]init];
//        [self.navigationController pushViewController:resVC animated:NO];
        
//        betweenTime = [self hourMinuteSecond:-betweenDate.second];
//        self.titleLabel.text = [NSString stringWithFormat:@"距离今天\n%@\n已经过时",_setTimeString];
    }
    // 距离xxx点还有多久
    else
    {
        betweenTime = [self hourMinuteSecond:betweenDate.second];
//        self.titleLabel.text = [NSString stringWithFormat:@"距离今天\n%@\n还有",_setTimeString];
    }
    
    self.labelTime.text = betweenTime;
}


- (NSString *)hourMinuteSecond:(NSInteger)time
{
    NSString *timeString;
    
    // 秒
    timeString = [NSString stringWithFormat:@"%ld",time%60];
    if (timeString.length<2)
    {
        timeString = [NSString stringWithFormat:@"0%ld",time%60];
        
    }
    // 分
    time /= 60;
    if (time >10)
    {
        timeString = [NSString stringWithFormat:@"%ld            %@",time%60,timeString];
    }
    else if (time<10)
    {
        timeString = [NSString stringWithFormat:@"0%ld%@",time%60,timeString];
    }
    
    // 时
    time /= 60;
    if (time > 10)
    {
        timeString = [NSString stringWithFormat:@"%ld            %@",time%60,timeString];
        
    }else if (time<10)
    {
        timeString = [NSString stringWithFormat:@"0%ld            %@",time%60,timeString];
    }
    
    return timeString;
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
